<template>
  <div>
        <el-form :model="queryParams" ref="queryForm" :inline="true"  label-width="68px">
          <el-form-item label="课程名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="模糊查询"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="selectCourseInfo"
            />
          </el-form-item>
          <el-form-item label="课程行业" prop="industry">
            <el-select
              v-model="queryParams.industryId"
              placeholder="课程行业"
              clearable
              size="small"
              style="width: 240px"
               @change="selectCourseInfo"
            >
            <el-option
                v-for="dict in industryOptions"
                :key="dict.id"
                :label="dict.format"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
           <el-form-item label="课程类型" prop="type">
            <el-select
              v-model="queryParams.typeId"
              placeholder="课程类型"
              clearable
              size="small"
              style="width: 240px"
               @change="selectCourseInfo"
            >
            <el-option
                v-for="dict in typeOptions"
                :key="dict.id"
                :label="dict.format"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="selectCourseInfo">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>


        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="clickAdd"
            >新增</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="courseList" :header-cell-style="getRowClass">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="课程编号" align="center" key="id" prop="id"  />
          <el-table-column label="课程名称" align="center" key="name" prop="name"/>
          <el-table-column label="创建人" align="center" key="userName" prop="userName"/>
          <el-table-column label="行业" align="center" key="industryName" prop="industryName"/>
          <el-table-column label="类型" align="center" key="typeName" prop="typeName"/>
          <el-table-column label="收费" :formatter="chargeFormat" align="center" key="charge" prop="charge"/>
          <el-table-column label="价格 (元)" align="center" key="price" prop="price"/>
          <el-table-column label="学时" align="center" key="classHours" prop="classHours"/>
          <el-table-column label="项目简介" align="center" key="briefIntroduction" prop="briefIntroduction"/>
          <el-table-column label="创建时间" align="center" key="createTime" prop="createTime"/>
          <el-table-column label="状态" align="center" key="status">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="statusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="180"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope" >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-s-claim"
                @click="handleNode(scope.row)"
              >章节</el-button>
            </template>
          </el-table-column>
        </el-table>


        <pagination
          v-show="this.sum>0"
          :total="this.sum"
          :page.sync='queryParams.page'
          :limit.sync='queryParams.number'
          @pagination="selectCourseInfo(false)"
        />

        <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入课程名称" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程行业">
              <el-select v-model="form.industryId" placeholder="请选择">
                <el-option
                  v-for="dict in industryOptions"
                  :key="dict.id"
                  :label="dict.format"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程类型">
              <el-select v-model="form.typeId" placeholder="请选择">
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.id"
                  :label="dict.format"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="是否收费">
              <el-select v-model="form.charge" placeholder="请选择">
                <el-option
                  v-for="dict in chargeOptions"
                  :key="dict.id"
                  :label="dict.format"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格 (元)" prop="price">
              <el-input-number v-model="form.price" controls-position="right" :step="1" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程学时" prop="classHours">
              <el-input-number v-model="form.classHours" controls-position="right" :step="1" :min="1" />
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="选项描述" prop="briefIntroduction">
              <el-input type="textarea"
                :rows="4"
                v-model="form.briefIntroduction"
                placeholder="请输入选项描述" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="titleNodes" :visible.sync="openNode" width="90%" style="margin-left: 10%" :close-on-press-escape="false" :show-close="false" :close-on-click-modal ="false"  append-to-body>
       <nodeEditor :courseId="courseId" :openNode="openNode" v-on:closeMain="closeMain"></nodeEditor>
    </el-dialog>
  </div>
</template>

<script>
import {getIndustryApi,getTypeApi} from "@/api/system/common"
import {selectCourseInfoApi,updateCourseInfoStatusApi,deleteCourseInfoApi,updateCourseInfoApi,addCourseInfoApi} from "@/api/system/course"
import nodeEditor from '@/components/nodeEditor'
export default {
  name: "course",
  components: {
    nodeEditor
  },
  // components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      refreshTable: true,
      // 导出遮罩层
      exportLoading: false,
      // 角色表格数据
      courseList: null,
      //课程id
      courseId: undefined,
      sum: 0,
       // 弹出层标题
      title: "",
      titleNodes: undefined,
      // 是否显示弹出层
      open: false,
      openNode: false,
      form: {},
      // 状态数据
      industryOptions: [],
      typeOptions:[],
      chargeOptions:[{
        "id": 0,
        "format": "不收费"
      },{
         "id": 1,
        "format": "收费"
      }],
      // 查询参数
      queryParams: {
        page: 1,
        number: 10,
        name: undefined,
        industry: undefined,
        type: undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `角色编号`, visible: true },
        { key: 1, label: `角色名称`, visible: true },
        { key: 2, label: `状态`, visible: true },
        { key: 3, label: `创建时间`, visible: true }
      ],
      // 表单校验
      rules: {

      }
    };
  },
  watch: {

  },
  created() {
    this.selectCourseInfo();
    getIndustryApi().then(data=>{
      this.industryOptions = data.data;
    })
    getTypeApi().then(data=>{
      this.typeOptions = data.data;
    });
  },
  methods: {
    closeMain(val){
        this.openNode = val;
      },
    chargeFormat(row, column) {
      if (row.charge =='1') {
        return '是'
      } else  {
        return '否'
      }
    },
    getRowClass ({ rowIndex }) {
      if (rowIndex == 0) {
        return 'background:#afdfe4'
      } else {
        return ''
      }
    },
    //查询课程
    selectCourseInfo(){
      selectCourseInfoApi(this.queryParams).then(data=>{
        this.courseList = data.data.returnDate;
        this.sum = data.data.paginationData.sum;
        this.loading = false;
      }).catch(()=>{
        this.courseList = null;
        this.sum = 0;
        this.loading = false;
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      if (this.$refs["queryForm"]) {
		    this.$refs["queryForm"].resetFields();
	    }
      this.selectCourseInfo();
    },
    // 角色状态修改
    statusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$confirm('确认要"' + text + '""' + row.name + '"课程吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=> {
          updateCourseInfoStatusApi({
            "id": row.id,
            "status": row.status
          }).then(()=>{
            this.$message({
               type: 'success',
              message: '修改成功'
            });
          })
        }).catch(()=> {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除课程编号为"' + row.id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=> {
          deleteCourseInfoApi({
            "id": row.id
          }).then(data=>{
            this.selectCourseInfo();
            this.$message({
               type: 'success',
              message: '删除成功'
            });
          })
        })
    },
    handleNode(row) {
      this.titleNodes = row.name;
      this.courseId = row.id;
      this.openNode = true;

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      selectCourseInfoApi({
        "id": row.id,
        "page":1,
        "number":1
      }).then(data=>{
        this.title = "修改课程";
        this.form.id = row.id;
        this.form.name = row.name;
        this.form.status = row.status;
        this.form.briefIntroduction = row.briefIntroduction;
        this.form.industryId = row.industryId;
        this.form.typeId = row.typeId;
        this.form.charge = row.charge;
        this.form.price = row.price;
        this.form.classHours = row.classHours;
        this.open = true;
      })
    }, // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        status: undefined,
        industry: undefined,
        type: undefined,
        briefIntroduction: undefined,
        industryId: undefined,
        typeId: undefined,
        charge: undefined,
        price: undefined,
        classHours: undefined
      };
       if (this.$refs["form"]) {
		    this.$refs["form"].resetFields();
	    }
    },
    clickAdd(){
      this.title = "添加课程"
      this.open = true;
      this.reset();
    },
     /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCourseInfoApi(this.form).then(data=>{
              this.open = false;
              this.selectCourseInfo();
              this.reset();
              this.$message({
                type: 'success',
                message: '修改成功'
              });
            })
          } else {
            addCourseInfoApi(this.form).then(data=>{
              this.open = false;
              this.selectCourseInfo();
              this.reset();
              this.$message({
                type: 'success',
                message: '添加成功'
              });
            })
          }
        }
      });
    }, // 取消按钮
    cancel() {
      this.open = false;
    }
  }
};
</script>
